<?php
/**
 * @brief Ecpss 支付插件类
 * @class Ecpss
 * @package App
 * @subpackage  Driver.Payment
 * @author cjli
 */
class Ecpss extends Payment implements Payment_Interface
{
	public function __construct()
	{
		$this->_paymentInfo = PaymentModel::get_payment_info('Ecpss');
	}

  public function getSettingFields()
  {
    return array(
      'merno' => '商户号',
      'md5key' => '商户密钥',
      'currency' => array(
        'name' => '支付币种',
        'type' => 'select',
        'options' => array(
          '1' => '美元',
          '2' => '欧元',
          '4' => '英镑',
        ),
        'default' => '1',
      ),
      'language' => array(
        'name' => '语言',
        'type' => 'select',
        'options' => array(
          '1' => '中文',
          '2' => '英文',
        ),
        'default' => '2',
      ),
      
    );
  }

  public function getSubmitForm($info)
  {
    $MD5key = $this->_paymentInfo['config']['md5key'];		//MD5私钥
    $MerNo = $this->_paymentInfo['config']['merno'];					//商户号
    $BillNo = $orderInfo['order_sn'];		//[必填]订单号(商户自己产生：要求不重复)
    $Currency = $this->_paymentInfo['config']['currency'];					//[必填]交易币种1:代表美金2:欧元4:英镑
    $Amount = $orderInfo['pay_amount'];				//[必填]订单金额
    $Language = $this->_paymentInfo['config']['language'];					//[必填]语言2:代表英文；1：代表中文
    $ReturnURL =  url('payment/callback', array('pay' => 'Ecpss')); 			//[必填]返回数据给商户的地址(商户自己填写):::注意请在测试前将该地址告诉我方人员;否则测试通不过
    $Remark = $orderInfo['order_sn'];  //[选填]升级。
     
    $md5src = $MerNo.$BillNo.$Currency.$Amount.$Language.$ReturnURL.$MD5key;		//校验源字符串
    $MD5info = strtoupper(md5($md5src));		//MD5检验结果

	 //送货信息(方便维护，请尽量收集！如果没有以下信息提供，请传空值:'')
	 //因为关系到风险问题和以后商户升级的需要，如果有相应或相似的内容的一定要收集，实在没有的才赋空值,谢谢。
	 
    $shippingAddress = $orderInfo['delivery_address'];
			$shippingAddress .= $orderInfo['delivery_address2'] ? '&nbsp;&nbsp;'.$orderInfo['delivery_address2'] : null;
			$shippingAddress .= $orderInfo['delivery_address3'] ? '&nbsp;&nbsp;'.$orderInfo['delivery_address3'] : null;
	
		//账单地址选择传递
		$firstname = $orderInfo['delivery_first_name'] ;  //------------------------账单地址   姓
		
		$lastname = $orderInfo['delivery_last_name'];  //'-------------------账单地址的姓
	
		$email = $orderInfo['delivery_email']; //'----------账单地址的Email
	
	  $phone = ($orderInfo['delivery_phone'] ? $orderInfo['delivery_phone'] : $orderInfo['delivery_mobile']); //'---------------账单地址的固定电话
	
		$zipcode = $orderInfo['delivery_postcode']; //'----------------账单地址的邮编
	
		$address = $shippingAddress; //'-------------账单地址具体地址
	
		$city = $orderInfo['delivery_city'];// '--------------------账单地址所在城市
	
		$state = $orderInfo['delivery_province']; //'-------------------账单地址所在省或者州
	
		$country = $orderInfo['delivery_country'];// '-------------------账单地址所在国
	
	
		//发货地址尽量收集   可提高支付率
		$shippingFirstName = $orderInfo['delivery_first_name'];  //'-------------------收货人的姓
	
		$shippingLastName = $orderInfo['delivery_last_name']; //'-------------------收货人的名
	
		$shippingEmail = $orderInfo['delivery_email']; //'----------收货人的Email

    $shippingPhone = ($orderInfo['delivery_phone'] ? $orderInfo['delivery_phone'] : $orderInfo['delivery_mobile']); //'---------------收货人的固定电话

		$shippingZipcode = $orderInfo['delivery_postcode'];; //'----------------收货人的邮编
	
		$shippingAddress = $shippingAddress; //'-------------收货人具体地址
	
		$shippingCity = $orderInfo['delivery_city']; // '--------------------收货人所在城市
	
		$shippingSstate = $orderInfo['delivery_province']; //'-------------------收货人所在省或者州
	
		$shippingCountry = $orderInfo['delivery_country'];// '-------------------收货人所在国家

    $products = $orderInfo['order_sn'];// '------------------物品信息

    $html = array(
      '<form id="frm" action="https://security.sslepay.com/sslpayment" method="post" target="_blank">',
      '<input type="hidden" name="MerNo" value="' . $MerNo . '">',
			'<input type="hidden" name="Currency" value="' . $Currency . '">',
			'<input type="hidden" name="BillNo" value="' . $BillNo . '">',
			'<input type="hidden" name="Amount" value="' . $Amount . '">',
			'<input type="hidden" name="ReturnURL" value="' . $ReturnURL . '">',
			'<input type="hidden" name="Language" value="' . $Language . '">',
			'<input type="hidden" name="MD5info" value="' . $MD5info . '">',
			'<input type="hidden" name="Remark" value="' . $Remark . '">',
			'<input type="hidden" name="shippingFirstName" value="' . $shippingFirstName . '">',
			'<input type="hidden" name="shippingLastName" value="' . $shippingLastName . '">',
			'<input type="hidden" name="shippingEmail" value="' . $shippingEmail . '">',
			'<input type="hidden" name="shippingPhone" value="' . $shippingPhone . '">',
			'<input type="hidden" name="shippingZipcode" value="' . $shippingZipcode . '">',
			'<input type="hidden" name="shippingAddress" value="' . $shippingAddress . '">',
			'<input type="hidden" name="shippingCity" value="' . $shippingCity . '">',
			'<input type="hidden" name="shippingSstate" value="' . $shippingSstate . '">',
			'<input type="hidden" name="shippingCountry" value="' . $shippingCountry . '">',
			'<input type="hidden" name="products" value="' . $products . '">', 
      '<input type="submit" class="pay_button" value="' . L('_PAY_NOW_') . '">',
      '</form>',
    );
    return implode(PHP_EOL, $html);
  }

  public function callback()
  {
    //var_dump($_POST);
	
		//MD5私钥
	$MD5key = $this->_paymentInfo['config']['md5key'];

	//订单号
	$BillNo = $_POST["BillNo"];
	//币种
	$Currency = $_POST["Currency"];
	//金额
	$Amount = $_POST["Amount"];
	//支付状态
	$Succeed = $_POST["Succeed"];
	//支付结果
	$Result = $_POST["Result"];
	//取得的MD5校验信息
	$MD5info = $_POST["MD5info"]; 
	//备注
	$Remark = $_POST["Remark"];

	//校验源字符串
  $md5src = $BillNo.$Currency.$Amount.$Succeed.$MD5key;
  //MD5检验结果
	$md5sign = strtoupper(md5($md5src));

	$result = array(
      'verified' => false,
      'paidAmount' => $Amount,
      'paymentId' => $TradeNo,
      'orderNumber' => $BillNo,
      'updateOrderStatus' => false,
      'redirect' => '',
      'message' => '',
    );

	/* 验证 */
	if ($MD5info==$md5sign) {

		if ($Succeed == '88' || $Succeed == '1' || $Succeed == '9' || $Succeed == '19') {
				$result['verified'] = true;
				$result['updateOrderStatus'] = true;
				$result['message'] = L('_PAYMENT_SUCCESS_');
			} else {
				$result['message'] =  L('_PAYMENT_FAILURE_');
			}
		} else {
			$result['message'] =  L('_PAYMENT_VALIDATION_FAILS_');
		}
	return $result;
  }
}